"use strict";
/* Controllers */
app.controller("roleListController", ['$scope', '$compile' , '$location', '$rootScope', '$routeParams', 'roleListService', function ($scope, $compile, $location, $rootScope, $routeParams, roleListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };
    $scope.refreshPermission = function() {
        var vm = $scope.vm;
        var params = {};
        vm.result.ms = '...';
        var procFn = function (response) {
            vm.result.ms = response.ms;
            alert(response.data);
        };
        roleListService.refreshPermission(params,procFn, errorFn);
    }

    $scope.searchItems = function() {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 去除两端空格
        vm.p.search = vm.p.search.replace(/^\s+|\s+$/g,'');
        if(vm.p.search=='') {
            alert("请输入搜索条件");
            return;
        }
        vm.result.ms = '...';
        var params = {search: vm.p.search};
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };

        roleListService.listRole(params,procFn, errorFn);
    }

    $scope.listRole = function () {
        var vm = $scope.vm;
        var params = {};
        vm.result.ms = '...';
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        roleListService.listRole(params,procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'createTime');

        $scope.listRole();
    };

    $scope.initializeController();


    $scope.confirmDel=function(id,index){
        vm.toDelId=id
        vm.toDelIndex=index
        $('#myModal').modal('show')
    }

    $scope.ok=function(){
        var vm = $scope.vm;
        console.log("toDelId"+vm.toDelId)
        console.log("toDelIndex"+vm.toDelIndex)
        var params = {};
        params.id=vm.toDelId
        var procFn = function (response) {
            if (response.success && response.data) {
               $('#list_table').bootstrapTable('remove', {
                   field:'id',
                   values:[vm.toDelId]
               });
               alert("删除成功！")
            } else {
               alert(response.msg);
            }
        };
        roleListService.delRole(params, procFn, errorFn);
        $('#myModal').modal('hide')
    }

}]);


/* Services */
app.service('roleListService', ['ajaxService', function (ajaxService) {

    this.listRole = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/role/listRole.do", successFunction, errorFunction);
    };

    this.delRole = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/role/delete.do", successFunction, errorFunction);
    };

    this.refreshPermission = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/role/refreshPermission.do", successFunction, errorFunction);
    };

}]);

function roleOptFormat(value, row, index) {
        var optCont='<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel('+ row.id +','+index+') >删除</a>\n'
//        var optCont2='<a type="button" class="btn btn-xs btn-primary" href="#/star/user/role_config/'+row.id+'" target="_blank">修改</a>'
        var optCont2='<a type="button" class="btn btn-xs btn-primary" href="#/star/user/permission_role/'+row.functionLabels+'" target="_blank">修改</a>'
        return [
            optCont,
            optCont2
        ].join('');
}



function functionLabelFmt(value, row, index) {

    if(value == null) {
        value= '-';
    }
    if(row.functionName==null) {
        row.functionName = '-'
    }
    return row.functionName + '(' + value + ')';
}

function userIdsFormat(value, row, index) {

    if(row.roleName=='用户') {
        return row.userIds;
    }else {
        return row.userGroupIds;
    }
}